public class MileageUtil {
  static final Integer MAX_MILES_PER_DAY = 500;

  public static void areMilesAllowed(Mileage__c[] miles) {
    Double totalMiles = 0;
    String createdbyId = UserInfo.getUserId();
   
    /* adds miles that were created in previous requests for today */
    for(Mileage__c m:[select miles__c from Mileage__c where createdDate = TODAY and createdById = :createdbyId and miles__c != null]) {
      totalMiles += m.miles__c;
    }

    /* Totals the miles in the request */
    for (Mileage__c m:miles) {
      totalMiles += m.miles__c;
      if(totalMiles > MAX_MILES_PER_DAY) m.miles__c.addError('Mileage request exceeds daily limit(' + MAX_MILES_PER_DAY + ')');
    }
  }
}